package 题目集.并查集;

import java.util.*;

public class 程序自动分析 {
    static Map<Integer, Integer> parent = new HashMap<>();
    static Map<Integer, Integer> size = new HashMap<>();

    public static int find(int x) {
        if (parent.get(x) == null) {
            return x;
        }
        int root = find(parent.get(x));
        parent.put(x, root);
        return root;
    }

    public static void union(int x, int y) {
        int rootX = find(x);
        int rootY = find(y);
        if (rootX != rootY) {
            int sizeX = size.getOrDefault(rootX, 1);
            int sizeY = size.getOrDefault(rootY, 1);
            if (sizeX <= sizeY) {
                parent.put(rootX, rootY);
                size.put(rootY, sizeX + sizeY);
            } else {
                parent.put(rootY, rootX);
                size.put(rootX, sizeX + sizeY);
            }
        }
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int T = sc.nextInt();
        out:
        while (T-- > 0) {
            int n = sc.nextInt();
            parent.clear();
            size.clear();
            List<int[]> unions = new ArrayList<>();
            List<int[]> exclude = new ArrayList<>();
            for (int i = 0; i < n; i++) {
                int[] arr = {sc.nextInt(), sc.nextInt()};
                int k = sc.nextInt();
                switch (k) {
                    case 1:
                        unions.add(arr);
                        break;
                    case 0:
                        exclude.add(arr);
                        break;
                }
            }
            for (int[] union : unions) {
                union(union[0], union[1]);
            }
            for (int[] ints : exclude) {
                int x = ints[0];
                int y = ints[1];
                if (find(x) == find(y)) {
                    System.out.println("NO");
                    continue out;
                }
            }
            System.out.println("YES");
        }
    }
}
